<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Block {

	private $header;
	private $konten;
	private $width;
	private $editable = false;
	private $menu = array();
	private $form;
	private $submit_button;
	private $id = '';
	private $class = '';
	private $content_only = false;
	private $style = '';

	public function __construct($header = '',$id = '',$class = ''){
		$this->CI =& get_instance();
		$this->header = $header;
		$this->id = $id;
		$this->class = $class;
	}

	private function create_field($form_label,$form_input,$class = ''){
		if($class != '') $class = 'class = '.$class;
		$field = "
			<div $class >
				$form_label
				$form_input
			</div>
			";
		return $field;
	}

	private function create_editable($label,$data,$form_label,$form_input){
		$editable = "

			<div class='item info'>
				<dt>$label</dt>
				<dd>$data</dd>
			</div>

			<div class='item field' style='display: none' >
				$form_label
				$form_input
			</div>

			";

		return $editable;
	}

	public function set_id($id){
		$this->id = $id;
		return $this;
	}

	public function set_class($class){
		$this->class = $class;
		return $this;
	}

	public function set_content_only(){
		$this->content_only = true;
		return $this;
	}

	public function set_style($style){
		$this->style = $style;
		return $this;
	}

	public function set_form($form_name,$form_id = ''){

		list($tipe) = explode("_", $form_id, 2);

		if($form_id != '') $form_id = "id = $form_id";
		$this->form = form_open($form_name,$form_id);
		
		if($tipe == 'edit'){
			$this->submit_button = form_submit('ubah','Ubah',"class=field style=display:none");
		} else {
			$this->submit_button = form_submit('ok','OK');
		}

		return $this;
	}

	public function set_submit_button($name, $label, $etc){
		$this->submit_button = form_submit($name,$label,$etc);
	}

	public function set_header($header){
		$this->header = $header;
		return $this;
	}

	public function add_menu($label,$link = '',$etc = ''){
		$this->menu[] = anchor($link,$label,$etc);
		return $this;
	}

	public function add_clear(){
		$this->konten[] = "<div class='clear'></div>";
	}

	public function add_custom($tag,$isi,$id = '' ,$tambahan = ''){

		if($id != '')
			$id = "id = '$id'";

		$data = "<$tag $id $tambahan> $isi </$tag>";

		$this->konten[] = $data;
		return $this;
	}

	public function add_grid($grid = 3, $alpha = false, $omega = false){

		if(is_int($grid)){
			$grid = new Grid($grid,$alpha,$omega);
		} 

		if(is_object($grid)){
			if(get_class($grid) == 'Grid'){
				$this->konten[] = $grid;
			}
		} else return false;

		return $grid;

	}

	public function add_data($label,$data){
		$this->konten[] = "
			<div class='item info'>
				<dt>$label</dt>
				<dd>$data</dd>
			</div>
			";
		return $this;
	}

	public function add_editable_data($id,$label,$data,$form_input = '',$form_label = ''){

		if($form_input == '')
			$form_input = form_input($id,$data);

		if($form_label == '')
			$form_label = $label;

		$form_label = form_label($form_label,$id);

		$this->konten[] = $this->create_editable($label,$data,$form_label,$form_input);
		return $this;
	}

	public function add_editable_dropdown_data($id,$label,$options,$value){

		$form_label = form_label($label,$id);
		$form_input = form_dropdown($id,$options,$value);

		$data = $options[$value];

		$this->konten[] = $this->create_editable($label,$data,$form_label,$form_input);
		return $this;
	}

	public function add_editable_textarea_data($id,$label,$value,$form_value = ''){

		$form_label = form_label($label,$id);
		if($form_value != '')
			$form_input = form_textarea($id,$form_value);
		else $form_input = form_textarea($id,$value);

		$this->konten[] = $this->create_editable($label,$value,$form_label,$form_input);
		return $this;
	}

	public function add_editable_propinsi($propinsi){

		// mendapatkan option propinsi untuk dropdown
		$this->CI->config->load('dropdown_options');
		$option_propinsi = $this->CI->config->item('propinsi');

		$form_label = form_label('Propinsi','propinsi');
		$form_input = form_dropdown('propinsi',$option_propinsi,$propinsi);

		$this->konten[] = $this->create_editable('Propinsi',$propinsi,$form_label,$form_input);
		return $this;
	}

	public function add_editable_username($akun){
		
		$model = get_class($akun);
		$username = $akun->get_username();

		//bikin usernamenya jadi hanya integer
		$this->CI->load->library('generator');
		$number_username = $this->CI->generator->generate_int_username($model,$username);

		// header untuk username Customer
		$this->CI->config->load('username_header');
		$header = $this->CI->config->item($model);

		$form_label = form_label('ID','username');
		$form_input = form_input('username',$number_username);

		$this->konten[] = $this->create_editable('ID',$username,$form_label,$form_input);
		return $this;
	}

	public function add_editable_email($email){

		$data = mailto($email);
		$form_label = form_label('Email','email');
		$form_input = form_input('email',$email);

		$this->konten[] = $this->create_editable('Email',$data,$form_label,$form_input);
		return $this;
	}

	public function add_editable_website($website){

		if($website != '') $data = anchor('http://'.$website,$website);
		else $data = '';
		$form_label = form_label('Website','website');
		$form_input = form_input('website',$website);

		$this->konten[] = $this->create_editable('Website',$data,$form_label,$form_input);
		return $this;
	}

	public function add_editable_identitas($customer){
		
		// mendapatkan data untuk option dropdown
		$this->CI->config->load('dropdown_options');
		$option_identitas = $this->CI->config->item('jenis_identitas');

		/*
		jika jenis identitasnya ktp, maka yang akan ditampilkan KTP
		jika jenis identitasnya diatas 3 karakter, maka
		contoh paspor akan ditampilkan dalam bentuk Paspor
		*/
		if(strlen($customer->get_jenis_identitas())<=3)
			$jenis_identitas = strtoupper($customer->get_jenis_identitas());
		else $jenis_identitas = ucfirst($customer->get_jenis_identitas());

		$label = 'No '.$jenis_identitas;
		$data = $customer->get_no_identitas();
		$form_label = form_label('No Identitas');
		$form_input = 
			form_dropdown(
				'jenis_identitas',
				$option_identitas,
				$customer->get_jenis_identitas()
			).form_input('nomor_identitas',$customer->get_no_identitas());

		$this->konten[] = $this->create_editable($label,$data,$form_label,$form_input);
		return $this;
	}

	public function add_editable_agen($agen){
		// mendapatkan data untuk option dropdown
		$this->CI->config->load('dropdown_options');
		$option_agen = models\Agen::get_dropdown_options();

		$data = anchor('agen/detail/'.$agen->get_username(),$agen->get_nama());
		$form_label = form_label('Agen','agen');
		$form_input = form_dropdown('agen',$option_agen,$agen->get_id());

		$this->konten[] = $this->create_editable('Agen',$data,$form_label,$form_input);
		return $this;
	}

	public function add_editable_teknisi($teknisi, $label = 'Teknisi'){
		// mendapatkan data untuk option dropdown
		$this->CI->config->load('dropdown_options');
		$option_teknisi = models\Teknisi::get_dropdown_options();

		$data = anchor('teknisi/detail/'.$teknisi->get_username(),$teknisi->get_nama());
		$form_label = form_label($label,'teknisi');
		$form_input = form_dropdown('teknisi',$option_teknisi,$teknisi->get_id());

		$this->konten[] = $this->create_editable($label,$data,$form_label,$form_input);
		return $this;
	}

	public function add_editable_pengecekan(models\Detail_checklist $dc){
		$label = $dc->get_nama_objek();
		if($dc->get_remarks() != '')
			$data = $dc->get_remarks();
		else $data = strtoupper($dc->get_kondisi());

		$option_kondisi = array(
			'ok' => 'OK',
			'nok' =>'Not OK',
		);

		$color = '';
		if($dc->get_kondisi() == 'ok'){
			$color = 'style="color:green"';
		} else if($dc->get_kondisi() == 'nok'){
			$color = 'style="color:red"';
		}

		$form_label = form_label($label);
		$form_input = 
			form_dropdown('kondisi_'.$dc->get_id(),$option_kondisi,$dc->get_kondisi()).
			form_textarea('remarks_'.$dc->get_id(),$dc->get_remarks()).
			form_checkbox('deletee_'.$dc->get_id(),'ok',FALSE).
			"<span>delete</span>";

		$this->konten[] = "

			<div class='item info'>
				<dt>$label</dt>
				<dd $color >$data</dd>
			</div>

			<div class='item field' style='display: none' >
				$form_label
				$form_input
			</div>

			";

		return $this;
	}

	function add_editable_testimonial($testimonial){
		$label = 'Testimonial';

		if($testimonial == '') 
			$data = 'Belum ada testimoni dari customer';
		else $data = $testimonial;

		$form_label = form_label('Testimonial','testimonial');
		$form_input = form_textarea('testimonial',$testimonial);

		$this->konten[] = $this->create_editable($label,$data,$form_label,$form_input);
		return $this;
	}

	public function add_new_pengecekan(){

		$option_objek = models\Objek_pengecekan::get_dropdown_options();
		$option_objek['custom'] = 'Custom';

		$option_kondisi = array(
			'ok' => 'OK',
			'nok' =>'Not OK',
		);

		$nama = 
			form_label('+ objek','nama_objek_baru').
			form_dropdown('nama_objek',$option_objek).
			form_input('nama_objek_baru','','disabled = "" style=display:none').
			form_button('ok_baru','OK');

		$this->konten[] = "
			<div class='inside_block field' style='display:none'>
				<div class='item'> $nama </div>
			</div>
			";
		return $this;
	}

	public function add_device_info(models\Checklist $checklist){

		$option_jenis_device = models\Jenis_device::get_dropdown_options();

		$nama_dt = 'Nama Device';
		$nama_data = $checklist->get_nama_device();
		$nama_label = form_label($nama_dt);
		$nama_input = form_input('nama_device',$nama_data);

		$jenis_dt = 'Jenis Device';
		$jenis_data = $checklist->get_jenis_device()->get_nama();
		$jenis_label = form_label($jenis_dt);
		$jenis_input = form_dropdown('jenis_device',$option_jenis_device,$checklist->get_jenis_device()->get_id());

		$this->konten[] = "
			<div class='inside_block device_info' >
				<div class='item info'> <dt>$nama_dt</dt> <dd>$nama_data</dd> </div>
				<div class='item field' style='display:none'> $nama_label $nama_input </div>
				<div class='item info'> <dt>$jenis_dt</dt> <dd>$jenis_data</dd> </div>
				<div class='item field' style='display:none'> $jenis_label $jenis_input </div>
			</div>
			";
		return $this;
	}

	public function add_text_field($name,$label,$value = '',$class=''){

		$form_label = form_label($label,$name);
		$form_input = form_input($name,$value);

		$this->konten[] = $this->create_field($form_label,$form_input,$class);
		return $this;
	}

	public function add_password_field($name,$label,$value = '',$class=''){

		$form_label = form_label($label,$name);
		$form_input = form_password($name,$value);

		$this->konten[] = $this->create_field($form_label,$form_input,$class);
		return $this;
	}

	public function add_textarea_field($name,$label,$value = ''){

		$form_label = form_label($label,$name);
		$form_input = form_textarea($name,$value);

		$this->konten[] = $this->create_field($form_label,$form_input);
		return $this;
	}

	public function add_dropdown_field($name,$label,$options,$value = ''){

		$form_label = form_label($label,$name);
		$form_input = form_dropdown($name,$options,$value);

		$this->konten[] = $this->create_field($form_label,$form_input);
		return $this;
	}

	public function add_custom_field($label,$form_field,$class){

		$form_label = form_label($label);

		$this->konten[] = $this->create_field($form_label,$form_field,$class);
		return $this;
	}

	public function add_username_field($model){

		$this->CI->load->library('generator');
		$auto_username = $this->CI->generator->generate_int_username($model);

		$this->CI->config->load('username_header');
		$header = $this->CI->config->item($model);
		
		$form_label = form_label('ID');
		$form_input = 
			form_dropdown('tipe_username',array('auto' => 'auto','manual' => 'manual')).
			'<span>'.$header.'-</span>'.
			form_input('username',$auto_username);

		$this->konten[] = $this->create_field($form_label,$form_input);
		return $this;
	}

	public function add_identitas_field(){

		$this->CI->config->load('dropdown_options');
		$option_identitas = $this->CI->config->item('jenis_identitas');

		$form_label = form_label('No Identitas','no_identitas');
		$form_input = 
			form_dropdown('jenis_identitas',$option_identitas).
			form_input('no_identitas');

		$this->konten[] = $this->create_field($form_label,$form_input);
		return $this;
	}

	public function add_hidden_field($id,$data = ''){
		$this->konten[] = form_hidden($id,$data);
		return $this;
	}

	function __toString(){

		$string = '';

		if(!$this->content_only){
			$id = ''; $style = '';
			if($this->id != '') $id = "id='$this->id'";
			if($this->style != '') $style = "style='$this->style'";
			$string .= "<div class='block $this->class' $id $style >";
		}

		if($this->form){
			$string .= $this->form;
		}

		if(count($this->menu) > 0){
			$string .= "<div class='menu'>";
			foreach($this->menu as $link){
				$string .= $link;
			}
			$string .= "</div>";
		}

		if($this->header != ''){
			$string .= heading($this->header,4);
		}

		$string .= '<div class="clear"></div>';

		if($this->konten){
			foreach($this->konten as $isi){
				$string .= $isi;
			}
		}

		if($this->form){
			$string .= "<div class='clear'></div>";
			$string .= $this->submit_button;
			$string .= form_close();
		}

		if(!$this->content_only)
			$string .= "</div>";

		return $string;
	}

}
